package best.driver.android.View;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import best.driver.android.Beans.OrderAddresses;
import best.driver.android.Beans.OrderDetailInfo;
import best.driver.android.Beans.OrderStatus;
import best.driver.android.R;

import java.util.List;


/**
 * Created by xuefeng on 2014/7/11.
 */
public class SelectThreeDialog {
    final Context context;
    public Dialog ad;

    LinearLayout mLinearPhone[] = new LinearLayout[11];
    View mDivider[] = new View[10];
    TextView mTvName[] = new TextView[11];
    TextView mTvPhone[] = new TextView[11];

    public SelectThreeDialog(final Context context, List<OrderAddresses> addresses) {

        this.context = context;
        ad = new Dialog(context, R.style.dialog);
        //ad.requestWindowFeature(Window.FEATURE_NO_TITLE);
        ad.show();
        ad.setCancelable(true);
        //替换对话框窗口的布局
        Window window = ad.getWindow();
        window.setContentView(R.layout.dialog_eleven);
        mLinearPhone[0] = (LinearLayout) window.findViewById(R.id.item1);
        mLinearPhone[1] = (LinearLayout) window.findViewById(R.id.item2);
        mLinearPhone[2] = (LinearLayout) window.findViewById(R.id.item3);
        mLinearPhone[3] = (LinearLayout) window.findViewById(R.id.item4);
        mLinearPhone[4] = (LinearLayout) window.findViewById(R.id.item5);
        mLinearPhone[5] = (LinearLayout) window.findViewById(R.id.item6);
        mLinearPhone[6] = (LinearLayout) window.findViewById(R.id.item7);
        mLinearPhone[7] = (LinearLayout) window.findViewById(R.id.item8);
        mLinearPhone[8] = (LinearLayout) window.findViewById(R.id.item9);
        mLinearPhone[9] = (LinearLayout) window.findViewById(R.id.item10);
        mLinearPhone[10] = (LinearLayout) window.findViewById(R.id.item11);
        mDivider[0] = (View) window.findViewById(R.id.divider1);
        mDivider[1] = (View) window.findViewById(R.id.divider2);
        mDivider[2] = (View) window.findViewById(R.id.divider3);
        mDivider[3] = (View) window.findViewById(R.id.divider4);
        mDivider[4] = (View) window.findViewById(R.id.divider5);
        mDivider[5] = (View) window.findViewById(R.id.divider6);
        mDivider[6] = (View) window.findViewById(R.id.divider7);
        mDivider[7] = (View) window.findViewById(R.id.divider8);
        mDivider[8] = (View) window.findViewById(R.id.divider9);
        mDivider[9] = (View) window.findViewById(R.id.divider10);

        for(int i = 0; i < mLinearPhone.length; i++) {
            mTvName[i] = (TextView) mLinearPhone[i].findViewById(R.id.tv_name);
            mTvPhone[i] = (TextView) mLinearPhone[i].findViewById(R.id.tv_phone);
        }
        setCancelOut();
        for(int i = 0; i < addresses.size(); i++) {
            final OrderAddresses address = addresses.get(i);
            if(OrderStatus.RECEIVER_ADDRESS.equals(address.flag)) {
                mTvName[i].setText("卸货人");
            } else {
                mTvName[i].setText("提货人");
            }
            //mTvName[i].setText(TextUtils.isEmpty(address.name) ? "": address.name);
            mTvPhone[i].setText(TextUtils.isEmpty(address.phone)? "": address.phone);
            if(!TextUtils.isEmpty(address.phone)) {
                mLinearPhone[i].setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + address.phone));
                        context.startActivity(intent);
                    }
                });
            }
            if(i == addresses.size() - 1) {
                mLinearPhone[i].setVisibility(View.VISIBLE);
            } else {
                mLinearPhone[i].setVisibility(View.VISIBLE);
                mDivider[i].setVisibility(View.VISIBLE);
            }
        }
    }

    public void dismiss() {
        ad.dismiss();
    }

    public void setCancelNotClicked() {
        ad.setCanceledOnTouchOutside(false);
    }

    public void setCancelOut() {
        ad.setCanceledOnTouchOutside(true);
    }
}
